<template>
  <!-- 新增录入画面对话框-->
  <el-dialog
    :title="form.title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    @close="closeFn"
    :before-close="handleClose"
    v-if="openState"
  >
    <el-form ref="editForm" :model="form" :rules="rules" :disabled="form.disabled" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="优惠类型" prop="discountType">
            <el-select v-model="form.discountType" placeholder="" style="width: 100%" :disabled="form.isDisabled">
              <el-option
                v-for="dict in dictMap.FEE_DISCOUNT_TYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="discountStatus">
            <el-select v-model="form.discountStatus" placeholder="" style="width: 100%">
              <el-option
                v-for="dict in dictMap.FEE_DISCOUNT_STATUS"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="this.form.discountType === '1'">
        <el-col :span="12">
          <el-form-item label="起始时间" prop="beginTime">
            <el-time-picker
              v-model="form.beginTime"
              style="width: 100%"
              :picker-options="{selectableRange:`00:00:00-${form.endTime ? form.endTime : '23:59:59'}`}"
              value-format="HH:mm:ss"
              format="HH:mm:ss"
            ></el-time-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-time-picker
              v-model="form.endTime"
              style="width: 100%"
              :picker-options="{selectableRange:`${form.beginTime ? form.beginTime : '00:00:00'}-23:59:59`}"
              value-format="HH:mm:ss"
              format="HH:mm:ss"
              placeholder="结束时间">
            </el-time-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="this.form.discountType === '2'">
        <el-col :span="12">
          <el-form-item label="渠道类型" prop="channelType">
            <el-select v-model="form.channelType" placeholder="" style="width: 100%" :disabled="form.isDisabled">
              <el-option
                v-for="dict in dictMap.TXN_SOURCE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="this.form.discountType === '3'">
        <el-col :span="12">
          <el-form-item label="客户等级" prop="custLevel">
            <el-select v-model="form.custLevel" placeholder="" style="width: 100%" :disabled="form.isDisabled">
              <el-option
                v-for="dict in dictMap.FEE_CUST_LEVEL"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="this.form.discountType === '4'">
        <el-col :span="12">
          <el-form-item label="特殊客户" prop="spcust">
            <el-input v-model="form.spcust" placeholder="" :disabled="form.isDisabled" maxlength="32"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="优惠方式" prop="discountMethod">
            <el-select style="width: 100%;" v-model="form.discountMethod" placeholder="优惠方式">
              <el-option
                v-for="dict in dictMap.FEE_DISCOUNT_METHOD"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" v-if="this.form.discountMethod==='1'||this.form.discountMethod==='3'">
          <el-form-item label="优惠数值" prop="discountValue">
            <ht-amount-input
              v-model="form.discountValue"
              placeholder="优惠数值"
            ></ht-amount-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="this.form.discountMethod==='2'||this.form.discountMethod==='3'">
          <el-form-item label="优惠折扣(%)" prop="discountRate">
            <ht-amount-input
              v-model="form.discountRate"
              placeholder="优惠折扣"
            ></ht-amount-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" maxlength="60" show-word-limit/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="isLoading" v-if="form.isDetail">提交</el-button>
      <el-button type="primary" @click="reset" :loading="isLoading" v-if="form.isDetail">重置</el-button>
      <el-button @click="closeFn">关闭</el-button>


    </div>
  </el-dialog>
</template>
<script>
import { save, update } from "@/api/cnaps/tips/FeeItemManage/PreferentialFeeManage.js";
import HtAmountInput from "@/views/components/HtAmountInput";

export default {
  name: "EditInfo",
  components: { HtAmountInput },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    form: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {},

  data() {
    return {
      disabled: false,
      isLoading: false,
      // 是否显示弹出层
      openState: false,
      // 表单校验
      rules: {
        discountType: [{ required: true, message: "不能为空", trigger: "change" }],
        discountStatus: [{ required: true, message: "不能为空", trigger: "change" }],
        channelType: [{ required: true, message: "不能为空", trigger: "change" }],
        custLevel: [{ required: true, message: "不能为空", trigger: "change" }],
        time: [{ required: true, message: "不能为空", trigger: "change" }],
        spcust: [{ required: true, message: "不能为空", trigger: "change" }],
        discountMethod: [{ required: true, message: "不能为空", trigger: "change" }],
        discountValue: [{ required: true, message: "不能为空", trigger: "change" }],
        discountRate: [{ required: true, message: "不能为空", trigger: "change" }],
        beginTime:[{ required: true, message: "不能为空", trigger: "change" }],
        endTime:[{ required: true, message: "不能为空", trigger: "change" }],
      },
    };
  },
  mounted() {},

  methods: {
    // 表单重置
    reset() {
      this.resetForm("editForm");
    },
    closeFn() {
      this.openState = false;
      this.reset();
      this.$set(this.form,"discountType","");
      this.$set(this.form,"discountStatus","");
      this.$set(this.form,"beginTime","");
      this.$set(this.form,"endTime","");
      this.$set(this.form,"channelType","");
      this.$set(this.form,"custLevel","");
      this.$set(this.form,"spcust","");
      this.$set(this.form,"discountMethod","");
      this.$set(this.form,"discountValue","");
      this.$set(this.form,"discountRate","");
      this.$set(this.form,"remarks","");
    },

    show() {
      this.openState = true;
    },
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid && this.form.isDisabled) {
          if(this.form.discountMethod==='1'){
            this.$set(this.form,"discountRate","")
          }if(this.form.discountMethod==='2'){
            this.$set(this.form,"discountValue","")
          }
          this.isLoading = true;
          update(this.form)
            .then(() => {
              this.isLoading = false;
              this.msgSuccess("修改成功");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
            });
        } else if (valid && !this.form.isDisabled) {
          if(this.form.discountMethod==='1'){
            this.$set(this.form,"discountRate","")
          }if(this.form.discountMethod==='2'){
            this.$set(this.form,"discountValue","")
          }
          this.isLoading = true;
          save(this.form)
            .then(() => {
              this.isLoading = false;
              this.msgSuccess("新增成功");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
            });
        }
      });
    },

    handleClose(done) {
      if(this.form.isDetail){
        this.$confirm("确认关闭？")
          .then((_) => {
            this.reset();
            done();
          })
          .catch((_) => {});
      }else{
        this.reset();
        done();
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
